package com.gcb.xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
 * 该类为用SAX解析一个xml文件的示例
 * 
 * @author Cre.Gu
 * 
 */
public class SAXParserXml extends DefaultHandler {
	// 定义SAX解析器工厂获取解析器解析XML文件
	private SAXParserFactory sapf = SAXParserFactory.newInstance();
	private SAXParser sap = null;
	// 解析的内容是书本信息
	private Book book;
	private List<Book> list;
	// 用于记录上一个节点的信息
	private String preTag;

	public SAXParserXml() {
		try {
			sap = sapf.newSAXParser();
			// reader = sap.getXMLReader();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void perser(String file) {
		InputStream is = SAXParserXml.class.getResourceAsStream(file);
		// System.out.println(is);
		try {
			sap.parse(is, this);
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		String file = "/com/gcb/xml/book.xml";
		SAXParserXml txp = new SAXParserXml();
		txp.perser(file);
		for (Book book : txp.list) {
			System.out.println(book);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#startDocument()
	 */
	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
		list = new ArrayList<Book>();
		System.out.println("开始解析");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#endDocument()
	 */
	@Override
	public void endDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.endDocument();
		System.out.println("结束解析");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// TODO Auto-generated method stub
		if ("book".equals(qName)) {
			list.add(book);
			book = null;
		}
		preTag = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String,
	 * java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// TODO Auto-generated method stub

		if ("book".equals(qName)) {
			book = new Book();
			book.setId(attributes.getValue(0));
		}
		preTag = qName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		// TODO Auto-generated method stub
		String text = new String(ch, start, length);
		if ("name".equals(preTag)) {
			book.setName(text);
		} else if ("price".equals(preTag)) {
			book.setPrice(text);
		}
	}

}
